// pages/system/help/help.js
import { Api } from "../../../utils/api.js"
import { Http } from '../../../utils/http.js'
let http = new Http()

Page({

  /**
   * 页面的初始数据
   */
  data: {
    showWithdraw: 0,
    withdrawSuccess: 0,
    time: '',
    currentTime: getApp().globalData.countDown <= 0 ? 60 : getApp().globalData.countDown,
    disabled: false,
    suffix: '',

    phoneText: null,
    codeText: null,
    registBtnDisabled:false,

    helpContents: [],
    isProxy: 0
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      time: parseInt(getApp().globalData.countDown) <= 0 ? '发送' : getApp().globalData.countDown + 's',
      currentTime: getApp().globalData.countDown <= 0 ? 60 : getApp().globalData.countDown
    })
    if (this.data.currentTime > 0 && this.data.currentTime<60){
      this.timeChange()
    }
    this.getUserRole()
    this.getHelp()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },

  /** 
   * 自定义事件
   */

  //手机号
  getPhone: function (e) {
    this.data.phoneText = e.detail.value;
  },

  //验证码
  getCode: function (e) {
    this.data.codeText = e.detail.value;
  },

  // 去注册（弹窗）
  goRegist: function() {
    this.setData({
      showWithdraw: 1
    })
  },

  //隐藏弹窗
  closeWithdraw: function() {
    this.setData({
      showWithdraw: 0
    })
  },

  //获取验证码
  getCodeAction: function() {
    let _this = this;
    if (!_this.data.disabled) {
      var phoneVar = _this.data.phoneText
      if (phoneVar == null || phoneVar == undefined || phoneVar == '') { //手机号为空
        wx.showToast({
          title: '手机号码不能为空',
          icon: 'none'
        })
      }else {
        var myreg = /^(((13[0-9]{1})|(15[0-9]{1})|17[0-9]{1}|(18[0-9]{1}))+\d{8})$/;
        if (!myreg.test(phoneVar)) {
          wx.showToast({
            title: '手机号码不合法',
            icon: 'none'
          })
        }else{
          _this.request_GetCode()
        }
      }
    }
  },

  //接口_获取验证码
  request_GetCode:function() {
    var that = this
    http.sendHttp(Api.get_vericode, {
      data: {
        phone: that.data.phoneText
      },
      success: function (res) {
        that.timeChange();//倒计时
        that.setData({
          disabled: true
        })
      }
    })
  },

  //倒计时
  timeChange(options){
    let that = this;
    let interval = null;
    let currentTime = getApp().globalData.countDown <= 0 ? 60 : getApp().globalData.countDown
    interval = setInterval(function () {
      currentTime--;
      that.setData({
        time: currentTime + 's',
        suffix: '',
      })
      getApp().globalData.countDown = currentTime;
      wx.setStorage({
        key: 'countDown',
        data: currentTime,
      })
      if (currentTime <= 0) {
        clearInterval(interval)
        that.setData({
          time: '发送',
          suffix: '',
          currentTime: 60,
          disabled: false
        })
        getApp().globalData.countDown = currentTime;
        wx.setStorage({
          key: 'countDown',
          data: currentTime,
        })
      } else{
        that.setData({
          time: currentTime + 's',
          suffix: '',
        })
        getApp().globalData.countDown = currentTime;
        wx.setStorage({
          key: 'countDown',
          data: currentTime,
        })
      }
    }, 1000)
  },


  //立即注册按钮事件
  registAgent: function () {
    let _this = this;
    var codeVar = _this.data.codeText;
    if (codeVar == null || codeVar == undefined || codeVar == '') {
      wx.showToast({
        title: '验证码不能为空',
        icon: 'none'
      })
    } else {
      _this.request_RegistAgent()
    }
  },

  //接口_注册成为分销客
  request_RegistAgent: function () {
    var that = this
    http.sendHttp(Api.become_proxy, {
      data: {
        phone: that.data.phoneText,
        vericode: that.data.codeText
      },
      success: function (res) {
        wx.showToast({
          title: '注册成功'
        })
        that.closeWithdraw(),
        that.setData({
          isProxy: 1
        })
      }
    }, 'POST')
  },

  getUserRole: function () {
    var that = this
    http.sendHttp(Api.get_user_role, {
      data: {},
      success: function (res) {
        that.setData({
          isProxy: res.data.obj.isProxy
        })
      }
    })
  },

  getHelp: function () {
    var that = this
    http.sendHttp(Api.get_help, {
      data: {},
      success: function (res) {
        that.setData({
          helpContents: res.data.obj.contents
        })
      }
    })
  },

  doNothing: function () {

  } 
})